'''
    自动化？
        8 * 9 * 8 * 9 = 100
        1.解决人工重复的执行问题的？
        2.自动化代替人工来做。
        3.前期条件：
            人工 > 自动化
        自动化流程
        前期：
            功能测试用例。
            自动化脚本对功能用例：80~90%
            1.自动化测试范围：
                80~90%
                依据：是否符合做自动化测试
            2.人员准备，周期，工具选型，环境搭建，编写脚本，风险及其把控 --> 《xxxxx系统自动化解决方案》
            3. 按照方案来执行
                周期：2周期：
                    第一周：需求评审，需求分析，
                        脚本编写，调试脚本和增强脚本，使之能进行自动化测试即可。
            4.准备测试数据：xlrd +  pymysql并进行后期的参数化测试
        中期：
            1.编写脚本
            2.增强和调试脚本
            3.执行测试

        后期：
            1.收集执行测试结果
            2.分析bug和定位
            3.脚本要随着版本的迭代进行维护
    工具选择：
        python + Selenium  + 谷歌浏览器(搜狗，360)103,火狐 + chromeDriver.exe    99
        python    + chromeDriver.exe +     谷歌(js)
二：
    自动化脚本的增强：
        昨天的脚本有大量重复执行中有几个问题：
            1.登陆的三支交易，每次登陆的三步操作都需要进行
            2.测试数据需要集中进行管理。
    1.重复的操作  提取形成页面逻辑操作，可以重复调用
    2.使用excel表格进行管理测试数据。ddt +  xlrd 实现参数化读取

'''
from lib2to3.pgen2 import driver
from selenium import webdriver
import time
#  打开浏览器
from selenium.webdriver.common.by import By
from unittest import TestCase
from LoginOperation import LoginOperation
from Loginzhong import Loginzhong
from ddt import ddt,data,unpack
from Logina import LoginData4
from Logina import LoginData5
from Logina import LoginData6
from Logina import LoginData7
from Logina import LoginData8
from Logina import LoginData9
from Logina import LoginData10
from Logina import LoginData11
from Logina import LoginData12
from Logina import LoginData13
@ddt
class TestLogin(TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Chrome()
        # 2.最大化窗口:防止无法定位
        self.driver.maximize_window()
        # 3.访问系统
        self.driver.get("http://localhost:90/")
    @data(*LoginData9().param)
    @unpack
    #买入支付宝期限理财成功
    def testLoginSuccess1(self, username, password, expect, pwd):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        time.sleep(1)
        clk = Loginzhong()
        result2 = clk.getMoney(self.driver)
        time.sleep(1)
        clk.buy(self.driver, pwd)
        result = clk.getSuccssBuy(self.driver)
        time.sleep(2)
        result3 = clk.getBalance(self.driver)
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        elif result2 != result3+100:
            result2 = str(result2)
            self.driver.save_screenshot("images\\" + result2 + "--.jpg")
            print("余额扣减错误！")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData10().param)
    @unpack
    #买入支付期限理财失败
    def testLoginEorror1(self, username, password, expect, pwd):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        print(username)
        time.sleep(1)
        clk = Loginzhong()
        result2 = 0
        result3 = 1
        try:
            result2 = clk.getMoney(self.driver)
            time.sleep(1)
        except:
            print("您没有绑定银行卡！")
        clk.buy(self.driver, pwd)
        result = clk.getSuccssBuy(self.driver)
        time.sleep(2)
        try:
            result3 = clk.getBalance(self.driver)
        except:
            print("您没有绑定银行卡！")
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        elif result2 != result3:
            a = str(result2)
            self.driver.save_screenshot("images\\" + a + "--.jpg")
            print("余额扣减错误！")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData4().param)
    @unpack
    #买入期限理财成功
    def testLoginSuccess2(self, username, password, expect, pwd):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        time.sleep(1)
        clk = Loginzhong()
        result2 = clk.getMoney(self.driver)
        time.sleep(1)
        clk.buy2(self.driver, pwd)
        result = clk.getSuccssBuy(self.driver)
        time.sleep(2)
        result3 = clk.getBalance(self.driver)
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.png")
        elif result2 != result3+5000:
            a = str(result2)
            self.driver.save_screenshot("images\\" + a + "--.png")
            print("余额扣减错误！")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData5().param)
    @unpack
    #买入期限理财失败
    def testLoginEorror2(self, username, password, expect, pwd):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        print(username)
        time.sleep(1)
        clk = Loginzhong()
        result2 = 0
        result3 = 1
        try:
            result2 = clk.getMoney(self.driver)
            time.sleep(1)
        except:
            print("您没有绑定银行卡！")
        clk.buy2(self.driver, pwd)
        result = clk.getSuccssBuy(self.driver)
        time.sleep(2)
        try:
            result3 = clk.getBalance(self.driver)
        except:
            print("您没有绑定银行卡！")
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        elif result2 != result3:
            a = str(result2)
            self.driver.save_screenshot("images\\" + a + "--.jpg")
            print("余额扣减错误！")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData11().param)
    @unpack
    # 买入基金理财成功
    def testLoginSuccess3(self, username, password, expect, pwd):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        time.sleep(1)
        clk = Loginzhong()
        result2 = clk.getMoney(self.driver)
        time.sleep(1)
        clk.buy3(self.driver, pwd)
        result = clk.getSuccssBuy(self.driver)
        time.sleep(2)
        result3 = clk.getBalance(self.driver)
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.png")
        elif result2 != result3 + 3000:
            a = str(result2)
            self.driver.save_screenshot("images\\" + a + "--.png")
            print("余额扣减错误！")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData12().param)
    @unpack
    # 买入基金理财失败
    def testLoginEorror3(self, username, password, expect, pwd):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        print(username)
        time.sleep(1)
        clk = Loginzhong()
        result2 =0
        result3 = 1
        try:
            result2 = clk.getMoney(self.driver)
            time.sleep(1)
        except:
            print("您没有绑定银行卡！")
        clk.buy3(self.driver, pwd)
        result = clk.getSuccssBuy(self.driver)
        time.sleep(2)
        try:
            result3 = clk.getBalance(self.driver)
        except:
            print("您没有绑定银行卡！")
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        elif result2 != result3:
            a = str(result2)
            self.driver.save_screenshot("images\\" + a + "--.jpg")
            print("余额扣减错误！")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData13().param)
    @unpack
    # 申请贷款成功
    def testLoginSuccess4(self, username, password, expect,money,day):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        time.sleep(1)
        clk = Loginzhong()
        clk.buy4(self.driver, money, day)
        result = clk.getSuccssTxt(self.driver)
        time.sleep(2)
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData6().param)
    @unpack
    # 正常申请贷款-银行审批通过测试
    def testLoginLoan4(self, username, password, expect,money,day, admin, pwd):
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        time.sleep(1)
        clk = Loginzhong()
        #获取初次余额
        result2 = clk.getMoney(self.driver)
        time.sleep(1)
        #申请贷款
        clk.buy4(self.driver, money, day)
        result = clk.getSuccssTxt(self.driver)
        time.sleep(2)
        #点击头像退出
        clk.alogin(self.driver)
        time.sleep(1)
        #登录管理员账号
        log.login(self.driver, admin, pwd)
        time.sleep(1)
        #审核贷款通过
        clk.agree(self.driver)
        time.sleep(1)
        #点击管理员头像退出
        clk.aalogin(self.driver)
        time.sleep(1)
        #重新进行用户登录
        log.login(self.driver, username, password)
        time.sleep(1)
        #获取贷款后余额
        result3 = clk.getBalance(self.driver)
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        elif result2 != result3 - 10000:
            a = str(result2)
            self.driver.save_screenshot("images\\" + a + "--.jpg")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData7().param)
    @unpack
    # 申请贷款失败
    def testLoginError11(self, username, password, expect,money,day):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        time.sleep(1)
        clk = Loginzhong()
        clk.buy4(self.driver, money, day)
        result = clk.getSuccssTxt(self.driver)
        time.sleep(2)
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    @data(*LoginData8().param)
    @unpack
    # 申请贷款失败
    def testLoginSuccess6(self, username, password, expect,money,day):
        time.sleep(1)
        # 登陆的逻辑操作
        log = LoginOperation()
        log.login(self.driver, username, password)
        time.sleep(1)
        clk = Loginzhong()
        clk.buy5(self.driver, money, day)
        result = clk.getTxt(self.driver)
        time.sleep(2)
        if result != expect:
            self.driver.save_screenshot("images\\" + expect + "--.jpg")
        else:
            print("登录用例通过！")
        self.assertEqual(expect, result)

    def tearDown(self) -> None:
        self.driver.quit()  # 自动关闭浏览器

